package ch.cryptobox.files;

import java.io.File;
import java.io.FileInputStream;
import ch.cryptobox.serivces.RemoteFileService;

public class FileSynchronizer implements FileWatcherListener {
	RemoteFileService service;

	public FileSynchronizer(final RemoteFileService service) {
		this.service = service;
	}


	@Override
	public void fileModified(final File file) {
		fileCreated(file);
	}

	@Override
	public void fileCreated(final File file) {	
		try {
			if(file.isDirectory())  {
				service.addDirectory(file.getAbsoluteFile().getCanonicalFile());
				return;
			}

			final FileInputStream reader = new FileInputStream(file);
			final int length  = (int)file.length();
			final byte[] buffer = new byte[length];
			reader.read(buffer);
			service.addFile(file, buffer);

		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void fileDeleted(final File file) {
		// TODO not supported
	}

}
